"""
 Copyright (c) 2022 Huawei Technologies Co.,Ltd.
"""

"""
Case Type   : Spq插件启动
Case Name   : Spq插件启动
Create At   : 2024/04/26
Owner       : oliver
Description :
            1.缺省配置shared_preload_libraries
            2.配置节点参数
            3.重启数据库
            4.配置节点权限
Expect      :
            1.配置成功
            2.配置成功
            3.重启成功
            4.配置成功
"""

import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger

class StartSpq(unittest.TestCase) :
    def setUp(self):
        self.log=Logger()
        self.log.info("-----------this is start spq-----------")
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.pri_userdb_spq = Node(node='PrimaryDbUser')
        self.std1_userdb_spq = Node(node='Standby1DbUser')
        self.constant = Constant()
        self.commsh_pri = CommonSH('PrimaryDbUser')
        self.commsh_std1 = CommonSH('Standby1DbUser')
        self.port = self.pri_userdb_spq.db_port
        self.IP1 = self.pri_userdb_spq.db_host
        self.IP3 = self.std1_userdb_spq.db_host
        self.node_name1 = 'dn_6001'
        self.node_name3 = 'dn_6003'
        self.conf_path = os.path.join(macro.DB_INSTANCE_PATH,macro.DB_PG_CONFIG_NAME)
        res = self.commsh_pri.exec_gs_ssh("cp $PGDATA/postgresql.conf $PGDATA/postgresql_spq.conf")
        self.log.info(res)
        self.assertEqual(res.count('[SUCCESS]'), 3 , 'failed to backup postgresql.conf')
        res = self.commsh_pri.exec_gs_ssh("cp $PGDATA/pg_hba.conf $PGDATA/pg_hba_spq.conf")
        self.log.info(res)
        self.assertEqual(res.count('[SUCCESS]'), 3 , 'failed to backup postgresql.conf')
        self.cluster_map = f"{self.node_name1}|{self.IP1}|{self.port},{self.node_name3}|{self.IP3}|{self.port}"

    def test_Spq(self):
        text = '--step1:配置shared_preload_libraries expect:成功--'
        self.log.info(text)
        guc_res = self.commsh_pri.execute_gsguc (
                  'set',self.constant.GSGUC_SUCCESS_MSG,
                  "shared_preload_libraries='security_plugin,spqplugin'")
        self.log.info(guc_res)
        text1 = self.assertTrue(guc_res,f'执行失败：{text}')
        self.log.info(text1)

        text = '--step2:配置节点参数 expect:成功--'
        self.log.info(text)
        guc_res = self.commsh_pri.execute_gsguc (
                  'set',self.constant.GSGUC_SUCCESS_MSG,f"pgxc_node_name='{self.node_name1}'",'',False,True,)
        self.log.info(guc_res)
        text1 = self.assertTrue(guc_res,f'执行失败：{text}')
        self.log.info(text1)

        guc_res = self.commsh_pri.execute_gsguc (
                  'set',self.constant.GSGUC_SUCCESS_MSG,f"pgxc_node_name='{self.node_name3}'",'',False,True,)
        self.log.info(guc_res)
        text1 = self.assertTrue(guc_res,f'执行失败：{text}')
        self.log.info(text1)

        text = '--step3:配置cluster_map,重启数据库 expect:成功--'
        self.log.info(text)
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        status = Primary_SH.check_cluster_core_status(status, wait_times=600)
        self.log.info(status)
        cmd = f"echo \\\"spqplugin.cluster_map='{self.cluster_map}'\\\" >> {self.conf_path}"
        self.log.info(cmd)
        conf_map=self.commsh_pri.exec_gs_ssh(cmd)
        self.log.info(conf_map)
        self.assertEqual(conf_map.count('[SUCCESS]'), 3)
        restart_res = self.commsh_pri.restart_db_cluster()
        self.log.info(restart_res)
        self.assertTrue(restart_res,f'重启数据库失败：{text}')

        text = '--step4:配置权限 expect:成功--'
        self.log.info(text)
        guc_res = self.commsh_pri.execute_gsguc (
                  'reload',self.constant.GSGUC_SUCCESS_MSG,'', 'all',False,False,macro.DB_INSTANCE_PATH,f'host all {self.pri_userdb_spq.db_user} {self.IP1}/32 trust')
        self.log.info(guc_res)
        self.assertTrue(guc_res,f'远程连接失败：{text}')

        guc_res = self.commsh_pri.execute_gsguc (
                  'reload',self.constant.GSGUC_SUCCESS_MSG,'', 'all',False,False,macro.DB_INSTANCE_PATH,f'host all {self.pri_userdb_spq.db_user} {self.IP3}/32 trust')
        self.log.info(guc_res)
        self.assertTrue(guc_res,f'远程连接失败：{text}')

    def tearDown(self):
        self.log.info('-----不恢复环境,执行sql用例-----')
        self.log.info(f"-----{os.path.basename(__file__)} end-----")